﻿$('#content_btnAddTo').click(function () {

    $('#content_CatelogueView tr').each(function (index) {
        var chk = $(this).find('input:checkbox');

        if (chk.is(':checked')) {
            var qty = $(this).find('input:text').val();
            var desc = $(this).children('td:first').text();


            console.log('Desc : ' + desc + ' ' + index);
            console.log('Qty : ' + qty);

            var isFound = true;
            if ($('#content_selectedTable').has('tr').length) {
                $('#content_selectedTable tr').each(function (index) {
                    if ($(this).children('td:first').text() == desc) {
                        console.log('find:replace value ' + index);
                        var preQty = parseInt($(this).find('input:text').val());
                        $(this).find('input:text').val(preQty + parseInt(qty));
                        isFound = true;
                        return false;
                    }
                    else {
                        console.log('not found :' + index);
                        isFound = false;
                    }

                });

                if (!isFound) {
                    console.log('not found, last append');
                    $('#content_selectedTable').append('<tr><td>' + desc + '</td><td><input type="text" id="qty" value=' + qty + ' style="width:80px"></td><td><a href="#">Remove</a></td></tr>');
                }
            }

            else {
                console.log('no tr : append ' + index);
                $('#content_selectedTable').append('<tr><td>' + desc + '</td><td><input type="text" id="qty" value=' + qty + ' style="width:80px"></td><td><a href="#">Remove</a></td></tr>');
            }

            console.log('------------------');
        }
    });
});